package com.supermap.dataservice.iot;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dataservice.util.MD5;
import com.supermap.dubbo.api.iot.IotUserBindService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import javax.annotation.Resource;

/**
 * @author aqua
 * @description 实现类
 * @date 2018年10月22 下午 12:43
 * @email 1301584033@qq.com
 */
@Service
public class IotUserBindSerivceImpl  implements IotUserBindService {

    protected static final Logger logger = LoggerFactory.getLogger(IotUserBindSerivceImpl.class);
    @Resource
    private IotUserBindMapper iotUserBindMapper;

    @Override
    public JSONUtil getIsBindUserId(String userId) {
        JSONUtil jsonUtil = new JSONUtil();
        Integer count=iotUserBindMapper.getIsBindUserId(userId);
        System.out.println(count);
         try {
             return iotUserBindMapper.getIsBindUserId(userId)>0?jsonUtil:jsonUtil.setMsg("操作失败!").setCode(100);
         }catch (Exception e){
             logger.error("{}","查看用户是否绑定失败!"+e.getMessage());
         }
         return  null;
    }

    @Override
    public JSONUtil getBindUserId(String userId) {
        JSONUtil jsonUtil = new JSONUtil();
        try {
            return   jsonUtil.setData(iotUserBindMapper.getBindUserId(userId));
        }catch (Exception e){
            logger.error("{}","根据用户ID查询绑定用户失败!"+e.getMessage());
        }
        return  null;
    }

    @Override
    public JSONUtil getAllVendor() {
        JSONUtil jsonUtil = new JSONUtil();
        try {
            return  jsonUtil.setData(iotUserBindMapper.getAllVendor());
        }catch (Exception e){
            logger.error("{}","获取所有厂家失败!"+e.getMessage());
        }
        return  null;
    }

    @Override
    public JSONUtil putVendorUser(String vendorid, String userId, String loginName, String password) {
        JSONUtil jsonUtil = new JSONUtil();
        try {
            return  jsonUtil.setData(iotUserBindMapper.putVendorUser(vendorid,userId,loginName,MD5.encode(password)));
        }catch (Exception e){
            logger.error("{}","插入用户绑定信息失败!"+e.getMessage());
        }
        return  null;
    }

    @Override
    public JSONUtil   saveVendorUser(String userId, String loginName, String password) {
        JSONUtil jsonUtil = new JSONUtil();
        try {

            return iotUserBindMapper.saveVendorUser(userId,loginName,MD5.encode(password))>0
                            ?jsonUtil:jsonUtil.setMsg("操作失败!").setCode(100);

        }catch (Exception e){
            logger.error("{}","修改物联网账号密码失败!"+e.getMessage());
        }
        return  null;
    }
}
